<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/11/8
 * Time: 9:27 AM
 */

namespace App\Library\User;


use App\Events\UserEvent;
use App\Models\User;

class PointManager
{
    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * @param User $user
     * @param $points
     * @param $from
     * @param $body
     * @param int $type 积分类型，1=茶多分，0=冰冻积分
     */
    public static function addPoints(User $user, $points, $from, $body, $type=1)
    {
        $manager = new static($user);
        $wallet = $manager->user->wallet()->firstOrCreate([]);
        if ($type == 1)
        {
            $wallet->points+= $points;
        } else {
            $wallet->bonus_points+= $points;
        }
        $wallet->save();

        $manager->user->pointRecords()->create([
            'points'=>$points,
            'type'=>$type,
            'from'=>$from,
            'body'=>$body,
            'trade_type'=>1
        ]);
    }

    /**
     * 增加消费积分
     * @param User $user
     * @param float $amount 消费金额
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public static function addConsumptionPoints(User $user, $amount)
    {
        $manager = new static($user);
        $points = $amount * setting('point_rate')/100;
        $wallet = $manager->user->wallet()->firstOrCreate([]);
        $wallet->bonus_points+= $points;
        $wallet->save();

        $manager->user->pointRecords()->create([
            'points'=>$points,
            'type'=>0,
            'from'=>'shopping',
            'body'=>'购物消费获得积分',
            'trade_type'=>1
        ]);

        event(new UserEvent($user, 'receivedbonuspoints'));

        //上级用户获得积分
        $relation = $manager->user->relation;
        if ($relation)
        {
            $upuser = $relation->upuser;
            if ($upuser)
            {
                $points = $amount * setting('point_up_rate')/100;
                $wallet = $upuser->wallet()->firstOrCreate([]);
                $wallet->bonus_points+= $points;
                $wallet->save();

                $upuser->pointRecords()->create([
                    'points'=>$points,
                    'type'=>0,
                    'from'=>'shopping',
                    'body'=>'下级用户购物消费获得积分',
                    'trade_type'=>1
                ]);
                event(new UserEvent($upuser, 'receivedbonuspoints'));

                if ($upuser->relation)
                {
                    $superuser = $upuser->relation->upuser;
                    if ($superuser)
                    {
                        $points = $amount * setting('point_super_rate')/100;
                        $wallet = $superuser->wallet()->firstOrCreate([]);
                        $wallet->bonus_points+= $points;
                        $wallet->save();

                        $superuser->pointRecords()->create([
                            'points'=>$points,
                            'type'=>0,
                            'from'=>'shopping',
                            'body'=>'下下级用户购物消费获得积分',
                            'trade_type'=>1
                        ]);
                        event(new UserEvent($superuser, 'receivedbonuspoints'));
                    }
                }
            }
        }
    }

    /**
     * @param User $user
     * @param $points
     * @param $from
     * @param $body
     * @param int $type 积分类型，1=茶多分，0=冰冻积分
     */
    public static function takeOffPoints(User $user, $points, $from, $body, $type=1)
    {
        $manager = new static($user);
        $wallet = $manager->user->wallet()->firstOrCreate([]);
        if ($type == 1)
        {
            if ($wallet->points >= $points)
            {
                $wallet->points-= $points;
                $wallet->save();

                $manager->user->pointRecords()->create([
                    'points'=>$points,
                    'type'=>1,
                    'from'=>$from,
                    'body'=>$body,
                    'trade_type'=>0
                ]);
            }
        } else {
            if ($wallet->bonus_points >= $points)
            {
                $wallet->bonus_points-= $points;
                $wallet->save();

                $manager->user->pointRecords()->create([
                    'points'=>$points,
                    'type'=>0,
                    'from'=>$from,
                    'body'=>$body,
                    'trade_type'=>0
                ]);
            }
        }
    }
}
